<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="flex mt10" style="width: 100%; background-color: white">
      <div>
        <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
          <h1 class="f16">{{ $t(`Print.RemainingInventoryQuery`) }}</h1>
          <Table
            :tableHeader="tableHeader"
            :tableData="tableData"
            class="mt10"
            :isLoading="isLoading"
            @currentRow="currentRow"
            :summaryData="summaryData"
            summary
            @sortChange="sortChange"
          />
        </el-card>
      </div>
      <div class="flex" style="flex-direction: column; width: 500px">
        <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
          <h1 class="f16">{{ $t(`Print.InquireAboutTheWarehousingDetails`) }}</h1>
          <Table
            :tableHeader="tableHeader2"
            :tableData="tableData2"
            class="mt10"
            :isLoading="isLoading2"
            :outTableHeight="180"
            :TableHeight="50"
            :summaryData="summaryData2"
            summary
          />
        </el-card>
        <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }" class="mt10">
          <h1 class="f16">{{ $t(`Print.InquireAboutTheOutboundDetails`) }}</h1>
          <Table
            :tableHeader="tableHeader3"
            :tableData="tableData3"
            :isLoading="isLoading2"
            :summaryData="summaryData3"
            summary
            :outTableHeight="180"
            :TableHeight="50"
          />
          <PageModule :PageData="{ pageSizeShow: true }" />
        </el-card>
      </div>
    </div>
    <el-card shadow="never" :body-style="{ padding: '5px' }">
      <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'RemainingInventoryQuery'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import Find from '@/components/Find/index.vue'
import { ElMessage } from 'element-plus'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'
const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入物料编码',
    inputValue: ''
  },
  {
    type: 'date',
    dateLabel: '入库日期',
    dateValue: ''
  },
  {
    type: 'date',
    dateLabel: '发料日期',
    dateValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入备注',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入库位',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入材料名称',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '分类',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'checkbox',
    checkboxLabel: '库存大于0',
    checked: true
  },
  {
    type: 'checkbox',
    checkboxLabel: '库存等于0',
    checked: true
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
]) as any

const tableHeader = ref([
  {
    sortable: 'custom',
    label: '物料编码',
    prop: 'mtlNo',
    width: '120'
  },
  {
    label: '材料颜色',
    prop: 'mtlColor',
    width: '180'
  },
  {
    sortable: 'custom',
    label: '材料名称',
    prop: 'mtlName',
    width: '210'
  },
  {
    label: '规格',
    prop: 'mtlSpec',
    width: '85px'
  },
  {
    label: '单位',
    prop: 'mtlUnit',
    width: '85px'
  },
  {
    sortable: 'custom',
    label: '入库日期',
    prop: 'recDate',
    width: '85px'
  },
  {
    sortable: 'custom',
    label: '发料日期',
    prop: 'traDate',
    width: '85px'
  },
  {
    label: '入库数量',
    prop: 'inQty',
    width: '85px'
  },
  {
    label: '发料数量',
    prop: 'outQty',
    width: '85px'
  },
  {
    label: '库存数量',
    prop: 'stkQty',
    width: '85px'
  }
])
// 表一模块
// 表一按钮
const findClicked = (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 8,
    num: 0
  },
  {
    index: 9,
    num: 0
  },
  {
    index: 10,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 表格计算函数
const cfmFlag = () => {
  const Unaudited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '库存大于0')
  const Audited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '库存等于0')
  if (!Unaudited || !Audited) return
  if (Unaudited.checked && Audited.checked) {
    return ''
  } else if (Unaudited.checked) {
    return 1
  } else if (Audited.checked) {
    return 0
  } else {
    ElMessage('必须至少选择一项')
    Unaudited.checked = true
    Audited.checked = true
    return ''
  }
}
// 表格排序
const order = ref('')
const prop2 = ref('')
const sortChange = (sort: any, prop: any, column: any) => {
  prop2.value = prop
  console.log(sort, prop, column)
  if (sort === 'ascending') {
    console.log('升序')
    order.value = '1'
  } else if (sort === 'descending') {
    console.log('降序')
    order.value = '0'
  } else if (sort === null) {
    order.value = ''
  }
  getTableList()
}
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/stkQuery/surplus/stk/temp', {
    mtlName: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入材料名称'
    )?.inputValue, //	材料名称
    mtlNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入物料编码'
    )?.inputValue, //	物料编码
    mtlType: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '分类')
      ?.selectValue, //	分类
    orderFile: prop2.value, //	排序字段
    orderType: order.value, //排序顺序（0. 逆序 || 1. 顺序）
    recDateEnd: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '入库日期'
    )?.dateValue?.[1], //	入库日期末
    recDateStart: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '入库日期'
    )?.dateValue?.[0], //	入库日期始
    remark: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入备注'
    )?.inputValue, //	备注
    stkFlag: cfmFlag(), //库存（0. 等于0 || 1.: ,// 大于0）
    stockNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入库位'
    )?.inputValue, //	库位
    traDateEnd: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '发料日期'
    )?.dateValue?.[1], //	发料日期末
    traDateStart: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '发料日期'
    )?.dateValue?.[0], //	发料日期始
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.dto
  total.value = tableDataList.total
  currentRow()
  summaryData.value[0].num = tableDataList.sum.inQtySum
  summaryData.value[1].num = tableDataList.sum.outQtySum
  summaryData.value[2].num = tableDataList.sum.stkQtySum
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
  getTableList2()
  getTableList3()
}

const tableHeader2 = ref([
  {
    label: '日期',
    prop: 'slipDate',
    width: '77px'
  },
  {
    label: '单据号码',
    prop: 'slipNo',
    width: '120'
  },
  {
    label: '单据类型',
    prop: 'slipReason',
    width: '77px'
  },
  {
    label: '指令',
    prop: 'lotNo',
    width: '77px'
  },
  {
    label: '数量',
    prop: 'qty',
    width: '77px'
  },
  {
    label: '备注',
    prop: 'remark',
    width: '77px'
  }
])

const tableHeader3 = ref([
  {
    label: '日期',
    prop: 'slipDate',
    width: '77px'
  },
  {
    label: '单据号码',
    prop: 'slipNo',
    width: '120'
  },
  {
    label: '单据类型',
    prop: 'slipReason',
    width: '77px'
  },
  {
    label: '指令',
    prop: 'lotNo',
    width: '77px'
  },
  {
    label: '数量',
    prop: 'qty',
    width: '77px'
  },
  {
    label: '备注',
    prop: 'remark',
    width: '77px'
  }
])
const summaryData2 = ref<summaryDataType[]>([
  {
    index: 4,
    num: 0
  }
])
const tableData2 = ref([])
const isLoading2 = ref(true)
// 分页器
const total2 = ref(0)
// 获取表数据
const getTableList2 = debounce(async () => {
  isLoading2.value = true
  let tableDataList = await service.post<any, any>(`/stkQuery/surplus/stk/detail?mtlNo=${currentTableRow.value.mtlNo}&slipType=I`)
  isLoading2.value = false
  tableData2.value = tableDataList.dto
  total2.value = tableDataList.total
  summaryData2.value[0].num = tableDataList.sum
}, 500)

const summaryData3 = ref<summaryDataType[]>([
  {
    index: 4,
    num: 0
  }
])
const tableData3 = ref([])
const isLoading3 = ref(true)
// 分页器
const total3 = ref(0)
// 获取表数据
const getTableList3 = debounce(async () => {
  isLoading3.value = true
  let tableDataList = await service.post<any, any>(`/stkQuery/surplus/stk/detail?mtlNo=${currentTableRow.value.mtlNo}&slipType=O`)
  isLoading3.value = false
  tableData3.value = tableDataList.dto
  total3.value = tableDataList.total
  summaryData3.value[0].num = tableDataList.sum
}, 500)

const getSelectList = async () => {
  const res = (await service.post('/stkQuery/surplus/stk/back', {
    mtlName: '',
    mtlNo: '',
    mtlType: '',
    orderFile: '',
    orderType: '',
    pageNum: '',
    pageSize: '',
    recDateEnd: '',
    recDateStart: '',
    remark: '',
    stkFlag: '',
    stockNo: '',
    traDateEnd: '',
    traDateStart: ''
  })) as any
  res.forEach((item: any) => {
    formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '分类')
      ?.selectOptions?.push({
        optionKey: item.typeNo,
        optionLabel: item.typeName,
        optionValue: item.typeNo
      })
  })
}
onMounted(() => {
  getSelectList()
})
</script>

<style lang="less" scoped></style>
